﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace System.Collections
{
    public class ArrayList:List<Object>
    {
        public T[] ToArray<T>()
        {
            var result = new T[this.Count];
            var idx = 0;
            foreach (var item in this)
            {
                try
                {
                    var value = Cast<T>(item);
                    result[idx] = value;
                }
                catch (Exception ex) 
                {
                    result[idx] = default(T);
                }
                idx++;
            }
            return result;
        }
        private static T Cast<T>(object o)
        {
            if (typeof(T) == typeof(String) && !(o is String))
                return Cast<T>(o.ToString());
            return (T)o;
        }

        internal static ArrayList CreateFrom(IList cloned)
        {
            var result = new ArrayList();
            foreach (var item in cloned)
                result.Add(item);
            return result;
        }
        internal static ArrayList CreateFrom(ICollection cloned)
        {
            var result = new ArrayList();
            foreach (var item in cloned)
                result.Add(item);
            return result;
        }

        internal static ArrayList Repeat(object value, int count)
        {
            var result = new ArrayList();
            for (int i = 0; i < count; i++)
                result.Add(value);

            return result;
        }

        internal ArrayList Clone()
        {
            return ArrayList.CreateFrom(this);
        }
    }
}
